package utils

import (
	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/canvas"
	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/layout"
	"gitee.com/huanminabc/go-utils/common"
)

//注意:
//1.不能直接设置组件的宽度,需要通过ColumnEmpty来挤压从而达到设置宽度的目的, 因为fyne的组件是自适应的不允许设置宽度
//2.高度可以通过小技巧的方式来设置Height,原理就是通过一个隐形的RowEmpty来撑开高度

// 空白行,高度为h
func RowEmpty(h float32) fyne.CanvasObject {
	gridWrapLayoutButton1 := container.New(layout.NewGridWrapLayout(fyne.NewSize(1, h)), layout.NewSpacer())
	return container.New(layout.NewVBoxLayout(), gridWrapLayoutButton1)
}

// 空白列,宽度为w
func ColumnEmpty(w float32) fyne.CanvasObject {
	gridWrapLayoutButton1 := container.New(layout.NewGridWrapLayout(fyne.NewSize(w, 1)), layout.NewSpacer())
	return container.New(layout.NewHBoxLayout(), gridWrapLayoutButton1)
}

// 根据内容自动适配width ,默认是最大宽度,我们可以通过下面的方法来让组件缩小到内容的宽度
func AutoWidth(content fyne.CanvasObject) fyne.CanvasObject {
	return container.New(layout.NewHBoxLayout(), content)
}

// 最大宽度 ,父级不能是NewHBox,否则就会退化为最小宽度
func MaxWidth(content fyne.CanvasObject) fyne.CanvasObject {
	return container.New(layout.NewVBoxLayout(), content)
}

// 组件高度调整
func Height(content fyne.CanvasObject, height float32) fyne.CanvasObject {
	return container.NewStack(RowEmpty(height), content)
}

// 设置组件大小, 解决fyne组件不能手动设置大小的问题
func FixedSize(content fyne.CanvasObject, width, height float32) fyne.CanvasObject {
	return container.New(layout.NewGridWrapLayout(fyne.NewSize(width, height)), content)
}

// 外边距 ,上下
func PaddingTopOrBottom(content fyne.CanvasObject, top, bottom float32) fyne.CanvasObject {
	box := container.NewVBox(RowEmpty(top), content, RowEmpty(bottom))
	return box
}

// 外边距 ,左右
func PaddingLeftOrRight(content fyne.CanvasObject, left, right float32) fyne.CanvasObject {
	return container.NewBorder(nil, nil, ColumnEmpty(left), ColumnEmpty(right), content)
}
func PaddingLeftOrRights(left, right float32, content ...fyne.CanvasObject) fyne.CanvasObject {
	var box []fyne.CanvasObject
	for _, v := range content {
		box = append(box, container.NewBorder(nil, nil, ColumnEmpty(left), ColumnEmpty(right), v))
	}
	return container.NewVBox(box...)
}

// 外边距 ,上下左右
func Padding(content fyne.CanvasObject, top, bottom, left, right float32) fyne.CanvasObject {
	return container.NewBorder(RowEmpty(left), RowEmpty(top), ColumnEmpty(right), ColumnEmpty(bottom), content)
}

// 水平线
func Line() fyne.CanvasObject {
	line := canvas.NewLine(common.HexToRGB("#DCDCDC"))
	line.StrokeWidth = 1 // 线粗细
	return line
}
